
#include <stdio.h>
#include <string.h>

#include "decl.h"


Obj*
newObj ()
{
	static int k = sizeof (Obj);
	Obj *o = malloc (k);
	return o;
}

Obj*
mkInt (long l)
{
	Obj *o = newObj ();
	o->t = TInt;
	o->u.l = l;
	return o;
}

Obj*
mkFloat (float f)
{
	Obj *o = newObj ();
	o->t = TFloat;
	o->u.f = f;
	return o;
}

Obj*
mkSymbol (char *s)
{
	Obj *o = newObj ();
	o->t = TSymbol;
	o->u.p = s;
	return o;
}

Obj*
mkPrimitive (Obj *(*f)(), int na, int evArgs)
{
	Obj 	*o = newObj ();
	Prim	*p = malloc (sizeof (Prim));

	p->na 		= na;
	p->evArgs	= evArgs;
	p->f		= f;

	o->t = TPrimitive;
	o->u.p = p;

	return o;
}

int 
isFalse (Obj *o)
{
    if (o && o->t == TBool && o->u.l == 0)
        return 1;
    return 0;
}

int
isTrue (Obj *o)
{
    if (o && o->t == TBool && o->u.l == 1)
        return 1;
    return 0;
}

int
eqStr (char *a, char *b)
{
	if (strcmp (a, b) == 0)
		return 1;
	return 0;
}

int
len (Obj *o)
{
	int i = 0;
	while (o) {
		i++;
		o = cdr (o);
	}
	return i;
}

Obj**
listToArray (Obj *o, int *r) 
{
	if (o && o->t == TList) {
		int l = len (o);
		Obj **x = malloc (sizeof (Obj*)*l),
			 *y = o;
		int i = 0;
		while (y) {
			x[i++] = car (y);
			y = cdr (y);
		}
		*r = i;
		return x;
	}
	*r = 0;
	return nil;
}

